package beans;

import java.sql.*;

import javax.naming.NamingException;
import javax.sql.*;
import tools.*;

public class UserBean {
	private int id = 0;
	private String name = null;
	private String password = null;
	private int role = 0;
	private String address = null;
	private String city = null;
	private String country = null;
	private String email = null;
	private String phone = null;

	private String message = "";

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	private Connection conn = null;
	private Statement stmt = null;
	private PreparedStatement pstmt = null;
	private ResultSet rs = null;

	public UserBean() {

	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public int getRole() {
		return role;
	}

	public void setRole(int role) {
		this.role = role;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public String getCity() {
		return city;
	}

	public void setCity(String city) {
		this.city = city;
	}

	public String getCountry() {
		return country;
	}

	public void setCountry(String country) {
		this.country = country;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
		setProfilebyEmail();
	}

	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	// Business Logic Methods

	public boolean setProfilebyEmail() {
		if (email == null)
			return false;
		try {
			conn = DBManager.getConnection();
			stmt = DBManager.getStatement(conn);
			String sqlCmd = String.format(
					"SELECT * FROM t_user WHERE t_user.Email = '%s' ", email);
			rs = stmt.executeQuery(sqlCmd);

			rs.last();
			if (rs.getRow() != 1) {
				return false;
			} else {
				setId(rs.getInt("ID"));
				setName(rs.getString("Name"));
				setPassword(rs.getString("Password"));
				setRole(rs.getInt("Role"));
				setAddress(rs.getString("Address"));
				setCity(rs.getString("City"));
				setCountry(rs.getString("Country"));

				setPhone(rs.getString("Phone"));
			}

		} catch (SQLException sqlException) {

		} catch (NamingException namingException) {

		} finally {
			DBManager.close(rs);
			DBManager.close(stmt);
			DBManager.close(conn);
		}
		return true;
	}

	public boolean updateProfilebyEmail() {
		if (email == null)
			return false;
		try {
			conn = DBManager.getConnection();
			stmt = DBManager.getStatement(conn);
			String sqlCmd = String.format(
					"SELECT * FROM t_user WHERE t_user.Email = '%s' ", email);
			rs = stmt.executeQuery(sqlCmd);

			rs.last();
			if (rs.getRow() != 1) {
				return false;
			} else {
				rs.updateString("Name", name);
				rs.updateString("Address", address);
				rs.updateString("City", city);
				rs.updateString("Country", country);
				rs.updateString("Phone", phone);
				rs.updateRow();
			}

		} catch (SQLException sqlException) {

		} catch (NamingException namingException) {

		} finally {
			DBManager.close(rs);
			DBManager.close(stmt);
			DBManager.close(conn);
		}
		return true;
	}

	public boolean updatePasswordbyEmail() {
		if (email == null)
			return false;
		try {
			conn = DBManager.getConnection();
			stmt = DBManager.getStatement(conn);
			String sqlCmd = String.format(
					"SELECT * FROM t_user WHERE t_user.Email = '%s' ", email);
			rs = stmt.executeQuery(sqlCmd);

			rs.last();
			if (rs.getRow() != 1) {
				return false;
			} else {
				rs.updateString("Password", password);
				rs.updateRow();
			}

		} catch (SQLException sqlException) {

		} catch (NamingException namingException) {

		} finally {
			DBManager.close(rs);
			DBManager.close(stmt);
			DBManager.close(conn);
		}
		return true;
	}

	public boolean isValid(String password) {
		return this.password.trim().equals(password.trim());
	}

	public boolean insert() {
		try {
			conn = DBManager.getConnection();
			stmt = DBManager.getStatement(conn);
			String sqlCmd = String.format("SELECT * FROM t_user");
			rs = stmt.executeQuery(sqlCmd);

			rs.moveToInsertRow();

			rs.updateString("Name", name);
			rs.updateString("Password", password);
			rs.updateInt("Role", 0);
			rs.updateString("Address", address);
			rs.updateString("City", city);
			rs.updateString("Country", country);
			rs.updateString("Email", email);
			rs.updateString("Phone", phone);

			rs.insertRow();

		} catch (SQLException sqlException) {
			return false;
		} catch (NamingException namingException) {
			return false;
		} finally {
			DBManager.close(rs);
			DBManager.close(stmt);
			DBManager.close(conn);
		}
		return true;
	}

	public boolean isUsed(String email) {
		if (email == null)
			return false;
		
		try {
			conn = DBManager.getConnection();
			stmt = DBManager.getStatement(conn);
			String sqlCmd = String.format(
					"SELECT * FROM t_user WHERE t_user.Email = '%s' ", email);
			rs = stmt.executeQuery(sqlCmd);
			
			if (rs.first()) {
				return true;
			} else {
				return false;
			}

		} catch (SQLException sqlException) {

		} catch (NamingException namingException) {

		} finally {
			DBManager.close(rs);
			DBManager.close(stmt);
			DBManager.close(conn);
		}
		
		return true;
	}
}
